<!-- 产品分类 -->
<template>
  <div class="product-category">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>
        <a href="/">产品管理</a>
      </el-breadcrumb-item>
      <el-breadcrumb-item>产品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row>
        <el-col :span="24">
          <div class="grid-content bg-purple">
            <el-button type="primary" @click="openAddCategory">添加分类</el-button>
          </div>
        </el-col>
      </el-row>
      <el-table
        :data="categoryDatas.data"
        style="width: 100%;margin-bottom: 20px;"
        row-key="id"
        border
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      >
        <el-table-column prop="id" label="序号" sortable width="180"></el-table-column>
        <el-table-column prop="title" label="标题" sortable width="180"></el-table-column>
        <el-table-column prop="img" label="分类图片">
          <template slot-scope="scope">
            <img :src="'http://8.136.129.225:3000/'+scope.row.img" width="60" alt>
          </template>
        </el-table-column>
        <el-table-column prop="is_show" label="是否可用">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.is_show"
              active-value="1"
              inactive-value="0"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-text="开"
              inactive-text="关"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="160">
          <template slot-scope="scope">
            <el-button @click="handleDeleClick(scope.row)" type="danger" size="small">删除</el-button>
            <el-button type="primary" size="small" @click="openEditDialog(scope.row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage4"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="5"
        background
        layout="total, sizes, prev, pager, next, jumper"
        :total="categoryDatas.count"
      ></el-pagination>
    </el-card>

    <!-- 添加分类 -->
    <el-dialog title="添加分类" :visible.sync="dialogaddCategoryFormVisible">
      <el-form :model="cateDatas">
        <el-form-item label="分类" label-width="80px">
          <el-cascader
            v-model="value"
            :props="{ checkStrictly: true }"
            :options="options"
            @change="handleChange"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="分类标题" label-width="80px">
          <el-input v-model="cateDatas.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="分类图片" label-width="80px">
          <el-upload
            class="avatar-uploader"
            action="http://8.136.129.225:3000/api/upload/img"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="'http://8.136.129.225:3000/' + imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="是否可用" label-width="80px">
          <el-switch
            v-model="cateDatas.is_show"
            active-color="#13ce66"
            inactive-color="#ff4949"
            inactive-value="0"
            active-value="1"
            active-text="可用"
            inactive-text="禁用"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogaddCategoryFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitAddCategory">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 修改分类 -->
    <el-dialog title="修改分类" :visible.sync="dialogEditCategoryFormVisible">
      <el-form :model="editDatas">
        <el-form-item label="分类" label-width="80px">
          <el-cascader
            v-model="value"
            :props="{ checkStrictly: true }"
            :options="options"
            @change="handleChange"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="分类标题" label-width="80px">
          <el-input v-model="editDatas.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="分类图片" label-width="80px">
          <el-upload
            class="avatar-uploader"
            action="http://8.136.129.225:3000/api/upload/img"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="'http://8.136.129.225:3000/' + imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="是否可用" label-width="80px">
          <el-switch
            v-model="editDatas.is_show"
            active-color="#13ce66"
            inactive-color="#ff4949"
            inactive-value="0"
            active-value="1"
            active-text="可用"
            inactive-text="禁用"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogEditCategoryFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitEditCategory">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import {
  addCategoryApi,
  editCategoryApi,
  categoryDeleApi
} from "@/api/product.js";
export default {
  //import引入的组件需要注入到对象中才能使用
  components: {},
  data() {
    //这里存放数据
    return {
      value: [],
      currentPage4: 1,
      dialogaddCategoryFormVisible: false,
      dialogEditCategoryFormVisible: false,
      page: 1,
      size: 5,
      cateDatas: {
        title: "",
        img: "",
        is_show: "1"
      },
      editDatas: {
        title: "",
        img: "",
        is_show: "1"
      },
      imageUrl: ""
    };
  },
  computed: {
    categoryDatas() {
      return this.$store.state.product.categoryListDatas;
    },
    options() {
      return this.$store.state.product.addCategoryListDatas;
    }
  },
  //方法集合
  methods: {
    getCategoryListDatas() {
      this.$store.dispatch("actChangeCategoryListDatas", {
        page: this.page,
        size: this.size
      });
    },
    getAddCategoryListDatas() {
      this.$store.dispatch("actChangeAddCategoryListDatas", {});
    },
    handleDeleClick(rowdata) {
      categoryDeleApi(rowdata).then(res => {
        console.log(res);
        if (res.data.status == 200) {
          this.getCategoryListDatas();
        } else {
          this.$message(res.data.msg);
        }
      });
    },
    openEditDialog(rowData) {
      console.log(rowData);
      this.imageUrl = rowData.img;
      this.editDatas = rowData;
      this.dialogEditCategoryFormVisible = true;
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.size = val;
      this.page = 1;
      this.getCategoryListDatas();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.page = val;
      this.getCategoryListDatas();
    },
    // 显示添加分类弹窗
    openAddCategory() {
      this.dialogaddCategoryFormVisible = true;
    },
    handleAvatarSuccess(res, file) {
      console.log(res.data.name); //public/uploads/hg_03.jpg
      console.log(file);
      this.imageUrl = res.data.name;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    // 级联菜单事件
    handleChange(value) {
      console.log(value);
      this.value = value;
    },

    // 点击添加弹窗的确定按钮
    submitAddCategory() {
      // console.log(this.value.pop());
      this.$set(this.cateDatas, "pid", this.value.pop());
      this.$set(this.cateDatas, "img", this.imageUrl);
      console.log(this.cateDatas);
      addCategoryApi(this.cateDatas).then(result => {
        console.log(result);
        this.dialogaddCategoryFormVisible = false;
        this.getCategoryListDatas();
      });
    },
    // 点击修改弹窗的确定按钮
    submitEditCategory() {
      this.$set(this.editDatas, "pid", this.value.pop());
      this.$set(this.editDatas, "img", this.imageUrl);
      editCategoryApi(this.editDatas).then(result => {
        console.log(result);
        this.dialogEditCategoryFormVisible = false;
        this.getCategoryListDatas();
      });
    }
  },
  mounted() {
    this.getCategoryListDatas();
    this.getAddCategoryListDatas();
  }
};
</script>
<style lang="less">
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 78px;
  height: 78px;
  line-height: 78px;
  text-align: center;
}
.avatar {
  width: 78px;
  height: 78px;
  display: block;
}
</style>